package com.jsm.tf.net.command;

import com.jsm.tf.entity.User;
import com.jsm.tf.net.message.request.CreateRoomMessage;
import com.jsm.tf.net.message.response.CreateRoomResponseMessage;
import com.jsm.tf.net.session.ISessionManager;
import com.jsm.tf.room.RoomDTO;
import com.jsm.tf.service.RoomService;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CreateRoomCommand implements Command<CreateRoomMessage> {
    @Resource
    private RoomService roomService;
    @Resource
    private ISessionManager sessionManager;

    @Override
    public void execute(CreateRoomMessage message, Channel channel) {
        User user = sessionManager.getUserByChannel(channel);
        long id = roomService.createRoom(message.getRoomName(), user.getId());
        sessionManager.enterRoom(id,user);
        CreateRoomResponseMessage responseMessage = new CreateRoomResponseMessage();
        responseMessage.setRoom(RoomDTO.builder().creatorNickName(user.getNickname()).name(message.getRoomName()).id(id).creatorId(user.getId()).build());
        responseMessage.setSeq(message.getSeq());
        channel.writeAndFlush(responseMessage);
    }
}
